package com.boolib.scheck.aop.common;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class ServerResponse<T> {


    private int status;

    private String msg;

    private T data;


    private ServerResponse(int status){
        this.status = status;
    }

    private  ServerResponse(int status ,String msg){
        this.status = status;
        this.msg = msg;
    }

    private ServerResponse(int status, T data){
        this.status = status;
        this.data = data;
    }

    private  ServerResponse(int status ,String msg, T data){
        this.status = status;
        this.msg = msg;
        this.data = data;
    }




    /**
     * success
     */

    @JsonIgnore
    public boolean isSuccess(){
        return this.status == ResponseCode.SUCCESS.getCode();
    }



    @JsonIgnore
    public boolean isExists(){
        return this.status != ResponseCode.SUCCESS.getCode();
    }






    public static <T> ServerResponse<T> createBySuccess(){
        return new ServerResponse<>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> ServerResponse<T> createBySuccessMessage(String msg){
        return  new ServerResponse<>(ResponseCode.SUCCESS.getCode(),msg);
    }

    public static <T> ServerResponse<T> createBySuccess(T data){
        return  new ServerResponse<>(ResponseCode.SUCCESS.getCode(),data);
    }


    public static <T> ServerResponse<T> createBySuccess(String msg,T data){
        return new ServerResponse<>(ResponseCode.SUCCESS.getCode(),msg,data);
    }


    /**
     * error
     */

    public static <T> ServerResponse createByError(){
        return new ServerResponse<>(ResponseCode.ERROR.getCode());
    }

    public static <T> ServerResponse createByErrorMessage(String errorMessage){
        return new ServerResponse(ResponseCode.ERROR.getCode(),errorMessage);
    }

    public static <T> ServerResponse createByError(int errorCode,String errorMessage){
        return new ServerResponse(errorCode,errorMessage);
    }


    public int getStatus() {
        return status;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }
}
